package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //1.参数检测
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.封装查询条件
        //2.1分页条件
        Page pageReq = new Page<>(dto.getPage(), dto.getSize());

        //2.2查询条件
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        //姓名不为空，模糊查询
        //使用commons.lang3工具包
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());
        }

        wrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = this.page(pageReq, wrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());

    }

    @Override
    public ResponseResult insert(AdChannel channel) {
// 1. 校验参数
        if(channel==null || StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        if(channel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能大于10个字符");
        }
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, channel.getName()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称已经存在");
        }
        // 2. 保存频道
        channel.setCreatedTime(new Date());
        this.save(channel);
        // 3. 返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        // 1. 校验参数
        if(adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        AdChannel channelGetById = getById(adChannel.getId());
        if(channelGetById == null){ // 频道不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        String inputName = adChannel.getName();
        if(StringUtils.isNotBlank(inputName)&&!inputName.equals(channelGetById.getName())){
            int count = count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, inputName));
            if(count > 0){ // 该频道已经存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
            }
        }
        // 2. 修改频道
        updateById(adChannel);
        // 3. 返回结果
        return ResponseResult.okResult();
    }


    @Transactional
    @Override
    public ResponseResult delete(Integer id) {
        // 1. 判断参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        // 2. 根据id查询频道信息
        AdChannel channel = getById(id);
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        // 3. 判断status是否为true   有效不能删除
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效无法删除");
        }
        // 4. 删除掉
//         int i = 1 / 0;

        removeById(id);
//        if(id==5){
//            throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id=5不让删除");
//        }
        // 5. 返回结果
        return ResponseResult.okResult();
    }
}
